package so.sao.code.service.domain.handler;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import so.sao.code.service.config.CodeMailProperties;
import so.sao.code.service.domain.event.CodeFileErrorLogEvent;
import so.sao.notice.sender.MailSender;

/**
 * event - handler
 */
@Component
public class ThirdPartyCodeFileHandler {

	@Autowired
	private CodeMailProperties codeMailProperties;

	/**
	 * 发送mail
	 * 
	 * @param event
	 */
	@EventListener
	@Async
	public void mailNotice(CodeFileErrorLogEvent event) {
		MailSender mailSender = MailSender.getDefaultMailSender();
		List<String> list = new ArrayList<>();
		for (String recipient : codeMailProperties.getRecipients()) {
			list.add(recipient);
		}
		mailSender.send(list, "【" + event.getTopic() + "-数据异常发生】", event.getErrorMessage()+System.lineSeparator()+event.toString());
	}

}
